package com.lagou;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class Monitor {

    // 请求次数
    private int numberOf = 0;
    // 请求耗时
    private List<Integer> time = new ArrayList<>();
    // 方法名称
    private String methodName;

    static Monitor getInstance() {
        Monitor monitor = new Monitor();
        return monitor;
    }

    public synchronized void add(int ms, String methodName) {
        numberOf++;
        time.add(ms);
        this.methodName = methodName;
    }

    public String getMethodName() {
        return methodName;
    }

    public int getTp(float f) {
        int round = Math.round(numberOf * f);
        time = time.stream().sorted((i1, i2) -> i1 - i2).collect(Collectors.toList());
        return time.get(round - 1);
    }

    public int getNumberOfRequests() {
        return numberOf;
    }

}
